﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using AjaxControlToolkit;
using System.Globalization;

public partial class CMSV2_Controls_GameFormat : System.Web.UI.UserControl
{
    private Game[] games;
    public Game[] Games
    {
        get { return this.games; }
        set
        {
            this.games = value;
            InitializeFormats();
        }
    }

    public Format[] SelectedFormats()
    {
        List<Format> selectedGames = new List<Format>();
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            CheckBox cbSelected = item.FindControl("uiCbFormat") as CheckBox;
            if (cbSelected.Checked)
            {
                Format form = new Format();
                form.Name = cbSelected.Text;
                form.Id = int.Parse(cbSelected.InputAttributes["value"]);
                selectedGames.Add(form);
            }
        }
        return selectedGames.ToArray();
    }

    public DateTime GetReleaseDate(int format)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).InputAttributes["value"].Equals(format.ToString(), StringComparison.Ordinal))
            {
                TextBox box = item.FindControl("uiTxtReleaseDate") as TextBox;
                CheckBox cbTBD = item.FindControl("uiCbReleaseToBeDetermined") as CheckBox;
                CheckBox cbCanceled = item.FindControl("uiCbReleaseCanceled") as CheckBox;
                if (cbCanceled.Checked)
                {
                    return new DateTime(6, 6, 6);
                }
                else if (cbTBD.Checked)
                {
                    return DateTime.MaxValue;
                }
                else if (box != null && !String.IsNullOrEmpty(box.Text))
                {
                    DateTime parseDate = DateTime.Parse(box.Text);

                    RadioButton rbMonth = item.FindControl("uiRbReleaseDateFormatMonth") as RadioButton;
                    RadioButton rbQuater = item.FindControl("uiRbReleaseDateFormatQuater") as RadioButton;
                    RadioButton rbYear = item.FindControl("uiRbReleaseDateFormatYear") as RadioButton;

                    if (rbMonth.Checked)
                    {
                        parseDate = new DateTime(parseDate.Year, parseDate.Month, 1).AddMonths(1).AddMinutes(-1);
                    }
                    else if (rbQuater.Checked)
                    {
                        int quater = ((parseDate.Month - 1) / 3) + 1;
                        parseDate = new DateTime(parseDate.Year, quater * 3, 1).AddMonths(1).AddMinutes(-1);
                    }
                    else if (rbYear.Checked)
                    {
                        parseDate = new DateTime(parseDate.Year, 1, 1).AddYears(1).AddMinutes(-1);
                    }

                    box.Text = parseDate.ToString("dd-MM-yyyy");

                    return parseDate;
                }
            }
        }

        return DateTime.MinValue;
    }

    public int GetReleaseDateFormat(int format)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).InputAttributes["value"].Equals(format.ToString(), StringComparison.Ordinal))
            {
                DateTime releaseDate = GetReleaseDate(format);

                if (releaseDate.Equals(DateTime.MinValue) || releaseDate.Equals(DateTime.MaxValue) || releaseDate.Equals(new DateTime(6, 6, 6)))
                    return 1;

                RadioButton rbMonth = item.FindControl("uiRbReleaseDateFormatMonth") as RadioButton;
                if (rbMonth.Checked)
                    return 2;

                RadioButton rbQuater = item.FindControl("uiRbReleaseDateFormatQuater") as RadioButton;
                if (rbQuater.Checked)
                    return 3;

                RadioButton rbYear = item.FindControl("uiRbReleaseDateFormatYear") as RadioButton;
                if (rbYear.Checked)
                    return 4;

                return 1;
            }
        }

        return 1;
    }

    public String GetPublisher(int format)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).InputAttributes["value"].Equals(format.ToString(), StringComparison.Ordinal))
            {
                TextBox box = item.FindControl("uiTxtPublisher") as TextBox;
                if (box != null)
                {
                    return box.Text;
                }
            }
        }
        return String.Empty;
    }

    public String GetDeveloper(int format)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).InputAttributes["value"].Equals(format.ToString(), StringComparison.Ordinal))
            {
                TextBox box = item.FindControl("uiTxtDeveloper") as TextBox;
                if (box != null)
                {
                    return box.Text;
                }
            }
        }
        return String.Empty;
    }

    public int GetMultiplayerInfo(int format)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).InputAttributes["value"].Equals(format.ToString(), StringComparison.Ordinal))
            {
                DropDownList box = item.FindControl("uiDdlMultiplayerInfo") as DropDownList;
                if (box != null)
                {
                    return int.Parse(box.SelectedValue);
                }
            }
        }
        return 0;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    }


    private void InitializeFormats()
    {
        using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
        {
            this.uiRepFormats.DataSource = service.GetAll();
            this.uiRepFormats.DataBind();
        }
    }

    protected void uiCbFormat_CheckedChanged(object sender, EventArgs e)
    {
        Panel formatDetails = ((sender as CheckBox).Parent.FindControl("uiPnlFormatDetails") as Panel);
        formatDetails.Visible = (sender as CheckBox).Checked;
    }

    protected void uiRepFormats_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox cbSelected = e.Item.FindControl("uiCbFormat") as CheckBox;
            TextBox tbReleaseDate = e.Item.FindControl("uiTxtReleaseDate") as TextBox;
            CheckBox cbTBD = e.Item.FindControl("uiCbReleaseToBeDetermined") as CheckBox;
            CheckBox cbCanceled = e.Item.FindControl("uiCbReleaseCanceled") as CheckBox;
            TextBox tbPublisher = e.Item.FindControl("uiTxtPublisher") as TextBox;
            TextBox tbDeveloper = e.Item.FindControl("uiTxtDeveloper") as TextBox;
            DropDownList ddlMulti = e.Item.FindControl("uiDdlMultiplayerInfo") as DropDownList;

            RadioButton rbDate = e.Item.FindControl("uiRbReleaseDateFormatDate") as RadioButton;
            RadioButton rbMonth = e.Item.FindControl("uiRbReleaseDateFormatMonth") as RadioButton;
            RadioButton rbQuater = e.Item.FindControl("uiRbReleaseDateFormatQuater") as RadioButton;
            RadioButton rbYear = e.Item.FindControl("uiRbReleaseDateFormatYear") as RadioButton;

            Panel pnlFormatDetails = e.Item.FindControl("uiPnlFormatDetails") as Panel;
            MutuallyExclusiveCheckBoxExtender mecbeReleaseCanceled = e.Item.FindControl("uiMecbeReleaseCanceled") as MutuallyExclusiveCheckBoxExtender;
            MutuallyExclusiveCheckBoxExtender mecbeReleaseToBeDetermined = e.Item.FindControl("uiMecbeReleaseToBeDetermined") as MutuallyExclusiveCheckBoxExtender;

            using (IMetadataService<MultiplayerInfo> service = ServiceFactory.GetMetadataService<MultiplayerInfo>())
            {
                ddlMulti.DataSource = service.GetAll();
                ddlMulti.DataBind();
            }

            cbSelected.Text = (e.Item.DataItem as Format).Name;
            cbSelected.InputAttributes.Add("value", (e.Item.DataItem as Format).Id.ToString());

            mecbeReleaseCanceled.Key = mecbeReleaseToBeDetermined.Key = cbSelected.ClientID;

            pnlFormatDetails.Visible = false;

            Game currentGame = null;
            if (this.Games != null)
            {
                currentGame = Games.FirstOrDefault(ga => ga.Format == (e.Item.DataItem as Format).Id);
            }

            if (currentGame != null)
            {
                cbSelected.Checked = true;
                pnlFormatDetails.Visible = true;

                tbReleaseDate.Text = String.Empty;
                if (!currentGame.ReleaseDate.Date.Equals(DateTime.MinValue.Date))
                {
                    if (currentGame.ReleaseDate.Date.Equals(DateTime.MaxValue.Date))
                    {
                        cbTBD.Checked = true;
                    }
                    else if (currentGame.ReleaseDate.Date.Equals(new DateTime(6, 6, 6).Date))
                    {
                        cbCanceled.Checked = true;
                    }
                    else
                    {
                        tbReleaseDate.Text = currentGame.ReleaseDate.ToString("dd-MM-yyyy");
                    }
                }

                rbDate.Checked = rbMonth.Checked = rbQuater.Checked = rbYear.Checked = false;

                switch (currentGame.ReleaseDateFormat)
                {
                    case 2:
                        rbMonth.Checked = true; break;
                    case 3:
                        rbQuater.Checked = true; break;
                    case 4:
                        rbYear.Checked = true; break;
                    default:
                        rbDate.Checked = true; break;
                }

                tbPublisher.Text = currentGame.Publisher;
                tbDeveloper.Text = currentGame.Developer;
                ddlMulti.SelectedValue = currentGame.MultiplayerInfo.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                cbTBD.Checked = true;
            }
        }
    }

    protected void uiCvGameFormatsSelectOne_ServerValidate(object source, ServerValidateEventArgs args)
    {
        foreach (RepeaterItem item in this.uiRepFormats.Items)
        {
            if ((item.FindControl("uiCbFormat") as CheckBox).Checked)
            {
                args.IsValid = true;
                return;
            }
        }
        args.IsValid = false;
    }

    protected void uiRxReleaseDate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime result;
        args.IsValid = DateTime.TryParse(args.Value, out result);
    }
}
